<template>
	<scroll-view class="container" scroll-y="true">
		<view class="example">
			<uni-forms ref="baseForm" :model="formData" :rules="rules" labelWidth="120px" label-position="top">
				<uni-forms-item label="项目" name="projectId" required>
					<uni-data-select v-model="formData.projectId" :localdata="projectOptions" />
				</uni-forms-item>
				<uni-forms-item label="地址" name="address" required>
					<uni-easyinput v-model="formData.address" placeholder="请输入房屋地址" suffixIcon="map-pin"
						@iconClick="handleAddress" />
				</uni-forms-item>
				<uni-forms-item label="建造年代" name="buildTime" required>
					<uni-easyinput type="number" v-model="formData.buildTime" placeholder="请输入建造年代" />
				</uni-forms-item>
				<uni-forms-item label="层数" name="layers" required>
					<uni-data-checkbox v-model="formData.layers" :localdata="layersRadios" />
					<uni-easyinput type="number" v-model="formData.layersText" v-if="showLayersText" />
				</uni-forms-item>
				<uni-forms-item label="结构形式" name="structureType" required>
					<uni-data-select v-model="formData.structureType" :localdata="structureTypeOptions" />
				</uni-forms-item>
				<uni-forms-item label="面积" name="floorSpace" required>
					<uni-easyinput type="digit" v-model="formData.floorSpace" placeholder="请输入建筑面积" />
				</uni-forms-item>
				<uni-forms-item label="混凝土" name="hunningtu" required>
					<uni-data-select v-model="formData.hunningtu" :localdata="hunningtuOptions" />
				</uni-forms-item>
				<uni-forms-item label="砌体砖" name="qitizhuan" required>
					<uni-data-select v-model="formData.qitizhuan" :localdata="qitizhuanOptions" />
				</uni-forms-item>
				<uni-forms-item label="砌筑砂浆" name="qizhushajiang" required>
					<uni-data-select v-model="formData.qizhushajiang" :localdata="qizhushajiangOptions" />
				</uni-forms-item>
				<uni-forms-item label="墙体厚度" name="qiangtihoudu" required>
					<uni-data-checkbox v-model="formData.qiangtihoudu" :localdata="qiangtihouduRadios" />
					<uni-easyinput type="number" v-model="formData.qiangtihouduText" v-if="showHouduText" />
				</uni-forms-item>
				<uni-forms-item label="砖抗压强度" name="zhuankangya" required>
					<uni-data-select v-model="formData.zhuankangya" :localdata="satisfyStatusOptions" />
				</uni-forms-item>
				<uni-forms-item label="砌筑砂浆抗压强度" name="qizhushajiangkangya" required>
					<uni-data-select v-model="formData.qizhushajiangkangya" :localdata="satisfyStatusOptions" />
				</uni-forms-item>
				<uni-forms-item label="混凝土抗压强度" name="huningtukangya" required>
					<uni-data-select v-model="formData.huningtukangya" :localdata="satisfyStatusOptions" />
				</uni-forms-item>
				<uni-forms-item label="顶点位移" name="dingdianweiyi" required>
					<uni-data-select v-model="formData.dingdianweiyi" :localdata="satisfyStatusOptions" />
				</uni-forms-item>
				<uni-forms-item label="受力裂缝及变形" name="liefengbianxing" required>
					<uni-data-select v-model="formData.liefengbianxing" :localdata="appearStatusOptions" />
				</uni-forms-item>
				<uni-forms-item label="外观质量缺陷" name="waiguanzhiliangquexian" required>
					<uni-data-select v-model="formData.waiguanzhiliangquexian" :localdata="appearStatusOptions" />
				</uni-forms-item>
				<uni-forms-item label="地基基础异常" name="dijiyichang" required>
					<uni-data-select v-model="formData.dijiyichang" :localdata="appearStatusOptions" />
				</uni-forms-item>
				<uni-forms-item label="上不结构异常" name="shangbuyichang" required>
					<uni-data-select v-model="formData.shangbuyichang" :localdata="appearStatusOptions" />
				</uni-forms-item>
				<uni-forms-item label="地基基础状况" name="dijizhuangkuang" required>
					<uni-data-select v-model="formData.dijizhuangkuang" :localdata="defectStatusOptions" />
				</uni-forms-item>
				<uni-forms-item label="承载能力验算" name="chengzailiyansuan" required>
					<uni-data-checkbox v-model="formData.chengzailiyansuan" :localdata="yesNoRadios" />
				</uni-forms-item>
			</uni-forms>
			<view class="button-group">
				<button type="success" size="mini" @click="reset">重置</button>
				<button class="bg-blue" size="mini" @click="formSubmit">完成</button>
			</view>
		</view>
	</scroll-view>
</template>

<script>
	import { getProjectList } from '@/api/busin/project/index.js'
	import amapFile from '@/plugins/amap-wx.130.js'
	import { getDictDatas } from '@/utils/dict.js'
	import { getHouseGeocode, loadVillages } from '@/api/busin/common.js'

	export default {
		data() {
			return {
				myAmapFun: undefined,
				formData: {
					projectId: undefined,
					province: '',
					city: '',
					district: '',
					township: '',
					villages: '',
					address: '',
					buildTime: 1970,
					structureType: 1,
					layers: 1,
					layersText: '',
					floorSpace: undefined,
					hunningtu: undefined,
					qitizhuan: undefined,
					qizhushajiang: undefined,
					qiangtihoudu: 180,
					zhuankangya: '0',
					qizhushajiangkangya: '0',
					huningtukangya: '0',
					dingdianweiyi: '0',
					liefengbianxing: '0',
					waiguanzhiliangquexian: '0',
					dijiyichang: '0',
					shangbuyichang: '0',
					dijizhuangkuang: '0',
					chengzailiyansuan: '0'
				},
				queryParams: {
					position: ''
				},
				projectOptions: [],
				villagesOptions: [],
				structureTypeOptions: [],
				layersRadios: [
					{
						value: 1,
						text: '单层'
					},
					{
						value: 9,
						text: '多层'
					}
				],
				showLayersText: false,
				hunningtuOptions: [],
				qitizhuanOptions: [],
				qizhushajiangOptions: [],
				qiangtihouduRadios: [
					{
						value: 180,
						text: '180mm'
					},
					{
						value: 240,
						text: '240mm'
					},
					{
						value: 370,
						text: '370mm'
					},
					{
						value: 999,
						text: '其他'
					}
				],
				showHouduText: false,
				satisfyStatusOptions: [],
				appearStatusOptions: [],
				defectStatusOptions: [],
				yesNoRadios: []
			}
		},
		created() {
			this.myAmapFun = new amapFile.AMapWX({key:'61f0725b4b1f70235b5cbbf15b06b7d8'});
			this.loadProjectList()
			getDictDatas('busin_jianzhujiegou').forEach((item, index) => {
				this.structureTypeOptions.push({
					value: item.value,
					text: item.label
				})
			})
			getDictDatas('busin_hunningtu').forEach((item, index) => {
				this.hunningtuOptions.push({
					value: item.value,
					text: item.label
				})
			})
			getDictDatas('busin_qitizhuan').forEach((item, index) => {
				this.qitizhuanOptions.push({
					value: item.value,
					text: item.label
				})
			})
			getDictDatas('busin_qizhushajiang').forEach((item, index) => {
				this.qizhushajiangOptions.push({
					value: item.value,
					text: item.label
				})
			})
			getDictDatas('busin_satisfy_status').forEach((item, index) => {
				this.satisfyStatusOptions.push({
					value: item.value,
					text: item.label
				})
			})
			getDictDatas('busin_appear_status').forEach((item, index) => {
				this.appearStatusOptions.push({
					value: item.value,
					text: item.label
				})
			})
			getDictDatas('busin_defect_status').forEach((item, index) => {
				this.defectStatusOptions.push({
					value: item.value,
					text: item.label
				})
			})
			getDictDatas('infra_boolean_integer').forEach((item, index) => {
				this.yesNoRadios.push({
					value: item.value,
					text: item.label
				})
			})
		},
		methods: {
			loadProjectList() {
				getProjectList().then((res) => {
					res.data.forEach((item) => {
						this.projectOptions.push({
							'value': item.id,
							'text': item.name
						})
					})
				})
			},
			handleAddress() {
				uni.showLoading({
					title: '定位中...'
				});
				this.myAmapFun.getRegeo({
				  success: (data) => {
					//成功回调
					let position = data[0].longitude + ','+ data[0].latitude
					this.queryParams.position = position
					getHouseGeocode(this.queryParams).then((res) => {
						this.formData.province = res.data.province
						// this.formData.provinceText = res.data.provinceText
						this.formData.city = res.data.city
						// this.formData.cityText = res.data.cityText
						this.formData.district = res.data.district
						// this.formData.districtText = res.data.districtText
						this.formData.township = res.data.township
						// this.formData.townshipText = res.data.townshipText
						this.formData.address = res.data.address
						// loadVillages(this.formData.township).then((resp) => {
						// 	resp.data.forEach(item => {
						// 		this.villagesOptions.push({
						// 			value: item.id,
						// 			text: item.name
						// 		})
						// 	})
						// 	uni.hideLoading();
						// })
					})
				  },
				  fail: (info) => {
					//失败回调
					uni.hideLoading();
				  }
				})
			},
		},
		watch: {
			'formData.layers' : function(newVal, oldVal) {
				if(newVal === 9){
					this.showLayersText = true
				}else {
					this.showLayersText = false
				}
			},
			'formData.qiangtihoudu' : function(newVal, oldVal) {
				if(newVal === 999){
					this.showHouduText = true
				}else {
					this.showHouduText = false
				}
			}
		}
	}
</script>

<style lang="scss">
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>